"""
    Player.py - Handles everything associated to the player
    Copyright (C) 2009  Matthew Baldwin <secretagentcook@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
"""

import DatabaseAbstractionLayer.DatabaseAbstraction
import Protocol
from sqlalchemy.orm import sessionmaker
import Core

class Player(object):
    """ Base Player commaPlayer.pynds """

   
    def __init__(self, sockRef=''):
        self.info = {}
        self.info['sockRef'] = None
        self.info['roomNum'] = None
        self.info['zoneNum'] = None
        self.info['name'] = ''
        self.info['password'] = ''
        self.info['login_state'] = None
        self.info['color'] = False
        
        #Database.dbAccess.Access()
        #self.session = Database.dbAccess.Access.Session()

    # Set and get functions

    def set_name(self,name):
        self.password = 'none'
        self.name = name
        self.addName = Database.dbAccess.Access.Players(self.name, self.password)
        self.session.add(self.addName)
        self.session.commit()
        self.info['name'] = self.name

    def get_name(self, name):
        self.name = name
        self.results = DatabaseAbstractionLayer.DatabaseAbstraction.Main.player_read(Core.Const.db_type,  self.name)
        
        
        print self.results 
        return self.results
       #TODO: self.results is password, change code to test.
       
    def set_password(self, name, password):
        self.name = self.info['name']
        self.password = password
        player = self.session.query(Players).filter_by(name=self.name).first()
        player.password = self.password
        self.session.commit()

    def get_password(self, name):
        self.name = name

        playerPass = Database.dbAccess.Access.access.session.query(Players).filter_by(name=self.name)
        return playerPass.password

    # Misc functions
    
    def setColor(self, flag):
        """Sets the color flag to true or false."""
        self.info['color'] = flag
        
    def getColor(self):
        """Returns true/false if the char wants color."""
        return self.info['color']

    # Write functions

    def get_sock_ref(self):
        return self.info['sockRef']

    def set_sock_ref(self, sockRef):
        self.info['sockRef'] = sockRef


    def prompt(self):
        """Returns a prompt to show to the user."""
        if self.getColor() == True:
            try:
                return Protocol.protocolHandler.process_text('\r\n'+self.info['name']+'> ')
            except AttributeError:
                pass
        else:
            try:
                return Protocol.protocolHandler.process_no_color('\r\n'+self.info['name']+'> ')
            except AttributeError:
                pass


    def write_with_prompt(self, data):
        """Writes a string to the socket with a prompt following."""
        if self.getColor() == True:
            try:
                self.sockRef.write(Protocol.protocolHandler.process_text('\r\n'+data+'\r\n'+self.prompt()))
            except AttributeError:
                pass
        else:
            try:
                self.sockRef.write(Protocol.protocolHandler.process_no_color('\r\n'+data+'\r\n'+self.prompt()))
            except AttributeError:
                pass

    def write_plain(self, data):
        """Writes data to the socket without a prompt following."""
                             
        if self.getColor():
            try:
                self.sockRef = self.get_sock_ref()
                self.sockRef.write(Protocol.protocolHandler.process_text(data))
            except AttributeError:
                pass

        else:
            try:
                #self.sockRef = self.get_sock_ref()
                self.sockRef.write(Protocol.protocolHandler.process_no_color(data))
            except AttributeError:
                self.sockRef = self.get_sock_ref()
                self.sockRef.write(data)
                

Player = Player()
